﻿using Common;
using DLL;
using gregn6Lib;
using NursingDocumentInfoSystem.Model;
using NursingFormSystem.Common;
using NursingFormSystem.Model;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace NursingFormSystem.InfantBaby
{
    public partial class NursingAllPrint : Form
    {
        #region 变量声明
        private GridppReport Report = new GridppReport();
        string Start;
        string End;
        #endregion

        #region 构造函数
        public NursingAllPrint(string StartDate, string EndDate)
        {
            InitializeComponent();
            Start = StartDate;
            End = EndDate;
            Report.LoadFromFile(System.Windows.Forms.Application.StartupPath + @"\GRFModel\Ydhl_GRF\NursingFormAllSum.grf");
            Report.ConnectionString = Utility.GetDatabaseConnectionString();
            Report.FetchRecord += new _IGridppReportEvents_FetchRecordEventHandler(ReportFetchRecord);
            axGRPrintViewer1.Report = Report;
        }
        #endregion

        #region 连接报表取数事件
        private void ReportFetchRecord()
        {
            string sql = "SELECT * FROM BNNursingDocsSystem_Develop.dbo.t_ClinicalNursing(NOLOCK) WHERE Submittime >= '{0}' AND Submittime < '{1} 23:59:59'";
            sql = string.Format(sql, Start, End);
            DataSet ds = SqlHelper.ExecuteDataset(sql, CommandType.Text);
            if (ds == null || ds.Tables.Count <= 0) return;
            DataTable tab = ds.Tables[0];
            DataRowCollection rows = tab.Rows;

            //给报表的
            DataTable dt = new DataTable();
            CreateDataTableHead(dt);
            for (int i = 0; i < rows.Count; i++)
            {
                string data = rows[i]["data"].ToString();
                ClinicalNursing model = GetModelByNursingFormData(data);
                string patientId = rows[i]["PatientId"].ToString().Replace("删除", "");
                string visitId = rows[i]["VISIT_ID"].ToString();
                string bedNo = GetBedNoByPidVid(patientId, visitId);

                dt.Rows.Add(
                    patientId,
                    visitId,
                    rows[i]["Id"].ToString(),
                    patientId,
                    rows[i]["DepId"].ToString(),
                    rows[i]["DepName"].ToString(),
                    bedNo,//床号
                    rows[i]["Submittime"].ToString(),//录入时间
                    rows[i]["Date"].ToString() + " " + rows[i]["Time"].ToString(),//测量时间
                    "",//测量方式
                    "",//测量项目代码
                    "体温,呼吸",//测量项目名称
                    model.Temper,//项目1的值
                    "℃",//项目1的单位
                    model.Pulse,//项目2的值
                    "次/分",//项目2的单位
                    model.Tendcontent,//病情和措施
                    "",//补充字段1
                    "",//补充字段2
                    rows[i]["PatientId"].ToString().IndexOf("删除") > 0 ? 0 : 1//1正常 0作废
                    );
            }
            Utility.FillRecordToReport(Report, dt);
        }

        /// <summary>
        /// 根据患者id和住院次数返回床号
        /// </summary>
        /// <param name="pid">患者id</param>
        /// <param name="vid">住院次数</param>
        /// <returns>床号</returns>
        private string GetBedNoByPidVid(string pid, string vid)
        {
            string res = string.Empty;
            string sql = "SELECT TOP 1 BED_NO FROM BNNursingDocsSystem_Develop.dbo.V_YDHL_PATIENT(NOLOCK) WHERE PATIENT_ID ='{0}' AND VISIT_ID = '{1}'";
            sql = string.Format(sql, pid, vid);
            object obj = SqlHelper.ExecuteScalar(sql);
            if (obj == DBNull.Value || obj == null) return res;
            if (string.IsNullOrEmpty(obj.ToString())) return res;
            res = obj.ToString();
            return res;
        }

        /// <summary>
        /// 根据护理单通用的data xml 字符串 
        /// 返回对应的xml 实体
        /// </summary>
        /// <param name="data"></param>
        private ClinicalNursing GetModelByNursingFormData(string data)
        {
            ClinicalNursing xmlModel = new ClinicalNursing();
            XmlDocument xmlDoc = new XmlDocument();
            StringReader Nurseingdata = new StringReader(data);
            xmlDoc.Load(Nurseingdata);
            XmlNode nursingrecord = xmlDoc.SelectSingleNode("/NursingRecordDocument");
            xmlModel.Temper = nursingrecord.ChildNodes[0].InnerText;
            xmlModel.Pulse = nursingrecord.ChildNodes[1].InnerText;
            xmlModel.HeartRate = nursingrecord.ChildNodes[2].InnerText;
            xmlModel.Breath = nursingrecord.ChildNodes[3].InnerText;
            xmlModel.Systolicpressure = nursingrecord.ChildNodes[4].InnerText;
            xmlModel.Diastolicpressure = nursingrecord.ChildNodes[5].InnerText;
            xmlModel.Spo2 = nursingrecord.ChildNodes[6].InnerText;
            xmlModel.StandUp = nursingrecord.ChildNodes[7].InnerText == "√" ? "1" : "0";
            xmlModel.Intakename = nursingrecord.ChildNodes[8].InnerText;
            xmlModel.Intakevalue = nursingrecord.ChildNodes[9].InnerText;
            xmlModel.Intakediway = nursingrecord.ChildNodes[10].InnerText;
            xmlModel.Dischargename = nursingrecord.ChildNodes[11].InnerText;
            xmlModel.Dischargevalue = nursingrecord.ChildNodes[12].InnerText;
            xmlModel.Tendcontent = nursingrecord.ChildNodes[13].InnerText;
            return xmlModel;
        }

        private static void CreateDataTableHead(DataTable dt)
        {
            dt.Columns.Add("patient_id", typeof(string));
            dt.Columns.Add("visit_sn", typeof(string));
            dt.Columns.Add("record_sn", typeof(string));
            dt.Columns.Add("medical_record_no", typeof(string));
            dt.Columns.Add("dept_code", typeof(string));
            dt.Columns.Add("dept_name", typeof(string));
            dt.Columns.Add("bed_no", typeof(string));
            dt.Columns.Add("record_datetime", typeof(string));
            dt.Columns.Add("test_datetime", typeof(string));
            dt.Columns.Add("test_method", typeof(string));
            dt.Columns.Add("item_code", typeof(string));
            dt.Columns.Add("item_name", typeof(string));
            dt.Columns.Add("item_result_value1", typeof(string));
            dt.Columns.Add("item_result_unit1", typeof(string));
            dt.Columns.Add("item_result_value2", typeof(string));
            dt.Columns.Add("item_result_unit2", typeof(string));
            dt.Columns.Add("observation_measures", typeof(string));
            dt.Columns.Add("extend_data1", typeof(string));
            dt.Columns.Add("extend_data2", typeof(string));
            dt.Columns.Add("record_status", typeof(string));
        }
        #endregion

        #region 窗体加载启动锐浪报表
        private void NursingAllPrint_Load(object sender, EventArgs e)
        {
            axGRPrintViewer1.Start();
        }
        #endregion
    }
}
